<template>
  <el-dialog v-model="visible" :title="!dataForm.appid ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="105px" @keyup.enter="submitHandle()">
        <el-form-item label="公众号名称" prop="name">
          <el-input v-model="dataForm.name" placeholder="公众号名称"></el-input>
        </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="公众号类型" prop="type">
            <ma-dict-select v-model="dataForm.type" placeholder="请选择" dict-type="official_account_type" style="width: 100%"></ma-dict-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否当前" prop="verified">
            <el-switch v-model="dataForm.verified" />
          </el-form-item>
        </el-col>
      </el-row>
        <el-form-item label="appId" prop="appid">
          <el-input v-model="dataForm.appid" placeholder="appId"></el-input>
        </el-form-item>
      <el-form-item label="appsecret" prop="secret">
        <el-input v-model="dataForm.secret" placeholder="appsecret"></el-input>
      </el-form-item>
      <el-form-item label="token" prop="token">
        <el-input v-model="dataForm.token" placeholder="token"></el-input>
      </el-form-item>
      <el-form-item label="aesKey" prop="aesKey">
        <el-input v-model="dataForm.aesKey" placeholder="aesKey"></el-input>
      </el-form-item>
      <el-form-item label="dataFormat" prop="dataFormat">
        <el-input v-model="dataForm.dataFormat" placeholder="dataFormat"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useAccountSubmitApi } from '@/api/wx/account'
const emit = defineEmits(['refreshdatalist'])
const visible = ref(false)
const dataFormRef = ref()

const dataForm = ref({
  aesKey: '',
  appid: '',
  dataFormat: '',
  name: '',
  secret: '',
  token: '',
  type: '',
  verified: ''
})

const init = (data?: number) => {
  dataForm.value = {}
  visible.value = true
  // 重置表单数据
  // if (dataFormRef.value) {
  // dataFormRef.value.resetFields()
  // }
  if (data !== undefined) {
    dataForm.value = data
  }
}

const dataRules = ref({
  name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
  appid: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
  secret: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }
    useAccountSubmitApi(dataForm.value).then(() => {
      ElMessage.success({
        message: '操作成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}

defineExpose({
  init
})
</script>

<style scoped>
.item_line {
  display: flex;
}
.item_line > div {
  flex: 1;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
